package com.toocms.jinjuhang.ui.mine.management_system.vip_center

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.blankj.utilcode.util.StringUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.ViewPageItem
import com.toocms.jinjuhang.databinding.FgtVipCenterBinding
import com.toocms.jinjuhang.ui.base.BaseFgt

class VipCenterFgt : BaseFgt<FgtVipCenterBinding, VipCenterModel>() {
    override fun onFragmentCreated() {
        topBar.setTitle(R.string.str_vip_center_list)
    }

    override fun getLayoutResId() = R.layout.fgt_vip_center

    override fun getVariableId() = BR.vipCenterModel

    override fun viewObserver() {
        if (null != binding.contentVp.adapter) return
        val membersInt = (arguments?.getString(Constants.BundleKey.KEY_MEMBERS) ?: "0").toInt()
        val commissionerInt =
            (arguments?.getString(Constants.BundleKey.KEY_COMMISSIONER) ?: "0").toInt()
        val content = arrayListOf(
            ViewPageItem(
                "${StringUtils.getString(R.string.str_vip)}${if (0 < membersInt) "($membersInt)" else ""}",
                VipListFgt().apply {
                    arguments=this@VipCenterFgt.arguments
                }
            ),
            ViewPageItem(
                "${StringUtils.getString(R.string.str_service_specialist)}${if (0 < commissionerInt) "($commissionerInt)" else ""}",
                ServiceSpecialistFgt().apply {
                    arguments=this@VipCenterFgt.arguments
                }
            )
        )

        binding.contentVp.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int): Fragment {
                return content[position].fgt
            }

            override fun getItemCount(): Int {
                return content.size
            }
        }
        binding.contentVp.isSaveEnabled = false
        TabLayoutMediator(
            binding.filterTl, binding.contentVp
        ) { tab, position -> tab.setText(content[position].title) }.attach()
    }

    override fun onResume() {
        super.onResume()
        binding.contentVp.adapter?.notifyDataSetChanged()
    }
}